package model.directory;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import model.logic.Disjunction;
/**
 * 
 * @author Marius Tempelmeier
 *
 */
public abstract class Service {
	
	/**
	 * Vector of provided operations. 
	 */
	private Vector<ServiceOperation> operations;

	
	public Service() {
		this.operations = new Vector<ServiceOperation>();
	}
	
	public Service(Vector<ServiceOperation> operations) {
		this.operations = operations;
	}

	private Vector<ServiceOperation> getOperations() {
		return this.operations;
	}
	
	public Object execute(List<Object>actualParameters, Disjunction requestedSematicConditions, Class<?> returnType) throws ExecutionException, NotFoundException {
		Iterator<ServiceOperation> i = this.getOperations().iterator();
		while (i.hasNext()) {
			ServiceOperation currentOperation = i.next();
			if (currentOperation.matchPreconditions(actualParameters) 
			    && currentOperation.matchPostconditions(requestedSematicConditions, returnType)) {
				return currentOperation.invoke(this, actualParameters);
			}
		}
		throw new NotFoundException("No suitable Operation found");
	}
	
	/**
	 * Adds an operation <tt>op</tt>. 
	 * @param op operation to be added
	 */
	public void addOperation(ServiceOperation op) {
		if (!this.operations.contains(op)) {
			this.operations.add(op);
		}
	}
	
	
}
